package com.ubb.damate.jasper.report.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletOutputStream;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lowagie.text.pdf.codec.Base64.OutputStream;
import com.ubb.damate.jasper.datasource.EclipseRuntimesDatasource;
import com.ubb.damate.jasper.datasource.EclipseUsageDataSource;
import com.ubb.damate.jasper.report.ReportType;
import com.ubb.damate.model.EclipseUsageModel;
import com.ubb.damate.model.RunTimesModel;
import com.ubb.damate.model.User;
import com.ubb.damate.service.RunTimesServiceLocal;
import com.ubb.damate.service.RunTimesServiceRemote;
import com.ubb.damate.service.UserServiceLocal;
import com.ubb.damate.service.impl.EclipseUsageService;
import com.ubb.damate.service.impl.RunTimesService;



public final class GenerateReportService {

	private static final Log log	= LogFactory.getLog(GenerateReportService.class);
	
	@EJB(beanName = "RunTimesService")
	private RunTimesServiceRemote	runTimesService;
	
	public final void generate(ReportType type, ServletOutputStream outputStream) {
		
		EclipseUsageService eclipseUsageService = new EclipseUsageService();
		try {
			InputStream reportIS = Thread.currentThread().getContextClassLoader().getResourceAsStream("/jasper/"+type.name()+".jasper");
			JRDataSource ds = null;
			
			switch (type) {
			
				case EclipseUsageReport:
					

					DataMock						dataMock		= new DataMock();
					Map<User, EclipseUsageModel> 	eclipseUsageMap = dataMock.eclipseUsageServiceMock(); //eclipseUsageService.getStats();
					List<Map<String, Object>> 		fields 			= new ArrayList<Map<String,Object>>();
					Iterator 						iterator 		= eclipseUsageMap.entrySet().iterator();
					
					while(iterator.hasNext()) {
						
						Map.Entry<User, EclipseUsageModel> 	entry 	= (Map.Entry<User, EclipseUsageModel>)iterator.next();
						Map<String, Object> 				row 	= new HashMap<String, Object>();
						row.put("user", entry.getKey().getUsername());
						
						for(int i = 0; i < 12; i++) {
							row.put("usage_" + (i + 1), (double)entry.getValue().getOpenSeconds().get(i));
						}
					
						fields.add(row);
					}
					
					ds = new EclipseUsageDataSource(fields);
					break;
				case EclipseRuntimesReport:
					
					/*Map<Object, Object> 		row	 	= new HashMap<Object, Object>();
					List<Map<Object, Object>> 	fields2 = new ArrayList<Map<Object,Object>>();
					
					row.put("day_number", 1);
					row.put("run_total", 10);
					row.put("run_fail", 3);
					row.put("run_success", 7);
					fields2.add(row);
					
					Map<Object, Object> row2 = new HashMap<Object, Object>();
					
					row2.put("day_number", 2);
					row2.put("run_total", 20);
					row2.put("run_fail", 13);
					row2.put("run_success", 7);
					
					fields2.add(row2);
					
					Map<Object, Object> row3 = new HashMap<Object, Object>();
					
					row3.put("day_number", 3);
					row3.put("run_total", 25);
					row3.put("run_fail", 17);
					row3.put("run_success", 8);
					
					fields2.add(row3);
					
					Map<Object, Object> row4 = new HashMap<Object, Object>();
					
					row4.put("day_number", 3);
					row4.put("run_total", 25);
					row4.put("run_fail", 17);
					row4.put("run_success", 8);
					
					fields2.add(row4);*/
				
					
					RunTimesModel runtimeData = runTimesService.getStats("fazy");
					Map<Object, Object> row = new HashMap<Object, Object>();
					List<Map<Object, Object>> fields2 = new ArrayList<Map<Object,Object>>();
					
					for(int i = 0; i < 12; i++) {
						
						int runSuccess 	= runtimeData.getNrOfSuccess().get(i);
						int runFail 	= runtimeData.getNrOfFail().get(i);
						row.put("run_success", runSuccess);
						row.put("run_fail", runFail);
						row.put("run_total", runSuccess + runFail);
						row.put("day_number", i);
						row.put("username", "Fazy");
						fields2.add(row);
					}
					
					ds = new EclipseRuntimesDatasource(fields2);
					
					break;
				case EclipseProjectHandlingReport:
					break;
					
			}
			JasperPrint jasperPrint = JasperFillManager.fillReport(reportIS, null, ds);
			JRPdfExporter exporter = new JRPdfExporter();
	        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
	        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	        exporter.exportReport();
		} catch (JRException e) {
			log.error("ERROR: "+e.getMessage(), e);
		}
	}
}
